package com.zxl.live.screen.ui.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/13.
 */

public class SelectionView<T> extends RecyclerView {

    public static final int MODE_NORMAL = 0;

    public static final int MODE_SELECT = 1;

    protected List<T> mDataList;

    protected List<T> mSelectList;

    protected int mSelectionMode;

    protected OnSelectionListener mListener;

    protected boolean mIsScroll;

    public SelectionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                switch (newState) {
                    case SCROLL_STATE_SETTLING:
                        mIsScroll = true;
                        break;
                    default:
                        mIsScroll = false;
                        Adapter adapter = recyclerView.getAdapter();
                        if (null != adapter) {
                            adapter.notifyDataSetChanged();
                        }
                        break;
                }
            }
        });
        mDataList = new ArrayList();
        mSelectList = new ArrayList();
    }

    public void clearSelect() {
        mSelectList.clear();
    }

    public void deselect(T paramT) {
        if (mSelectList.contains(paramT)) {
            mSelectList.remove(paramT);
        }
        if (mListener != null) {
            mListener.onSelectionChange(mSelectList.size(), getDataSize());
        }
        getAdapter().notifyDataSetChanged();
    }

    public void deselectAll() {
        mSelectList.clear();
        if (mListener != null) {
            mListener.onSelectionChange(mSelectList.size(), getDataSize());
        }
        getAdapter().notifyDataSetChanged();
    }

    public void enterSelectionMode() {
        if (mSelectionMode == 1) {
            return;
        }
        mSelectionMode = 1;
        if (null != mListener) {
            mListener.onSelectionModeChange(mSelectionMode);
        }
        getAdapter().notifyDataSetChanged();
    }

    public List<T> getSelectedList() {
        return mSelectList;
    }

    public boolean inselectionMode() {
        return mSelectionMode == 1;
    }

    public boolean isSelected(T paramT) {
        return mSelectList.contains(paramT);
    }

    public boolean isSelectedAll() {
        return mSelectList.size() == getDataSize();
    }

    public void leaveSelectionMode() {
        if (mSelectionMode == MODE_NORMAL) {
            return;
        }
        mSelectList.clear();
        mSelectionMode = 0;
        if (null != mListener) {
            mListener.onSelectionModeChange(mSelectionMode);
        }
        getAdapter().notifyDataSetChanged();
    }

    public void select(T paramT) {
        if (!mSelectList.contains(paramT)) {
            mSelectList.add(paramT);
        }
        if (mListener != null) {
            mListener.onSelectionChange(mSelectList.size(), getDataSize());
        }
        getAdapter().notifyDataSetChanged();
    }

    protected int getDataSize() {
        return mDataList.size();
    }

    public void selectAll() {
        mSelectList.clear();
        mSelectList.addAll(mDataList);
        if (mListener != null) {
            mListener.onSelectionChange(mSelectList.size(), getDataSize());
        }
        getAdapter().notifyDataSetChanged();
    }

    public void setDataList(List<T> paramList) {
        mDataList.clear();
        mDataList.addAll(paramList);
        getAdapter().notifyDataSetChanged();
    }

    public List<T> getDataList() {
        return mDataList;
    }

    public void setSelectionListener(OnSelectionListener listener) {
        mListener = listener;
    }

    public interface OnSelectionListener {

        void onLongClick();

        void onSelectionChange(int selectSize, int dataSize);

        void onSelectionModeChange(int mode);
    }

}
